package com.goods.business.service.imp;

import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductService;
import com.goods.common.model.business.Product;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    //加载物资列表
    @Override
    public PageVO getProductList(Integer pageNum, Integer pageSize, String categorys,String supplier, ProductVO productVO) {
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();

        //根据id模糊查询
        if (categorys != null && categorys.length() >0){
            String[] split = categorys.split(",");
            switch (split.length){
                case 1:{
                    productVO.setOneCategoryId(Long.parseLong(split[0]));
                    criteria.andEqualTo("oneCategoryId",productVO.getOneCategoryId());
                    break;
                }
                case 2:{
                    productVO.setOneCategoryId(Long.parseLong(split[0]));
                    productVO.setTwoCategoryId(Long.parseLong(split[1]));
                    criteria.andEqualTo("oneCategoryId",productVO.getOneCategoryId())
                            .andEqualTo("twoCategoryId",productVO.getTwoCategoryId());
                    break;
                }
                case 3:{
                    productVO.setOneCategoryId(Long.parseLong(split[0]));
                    productVO.setTwoCategoryId(Long.parseLong(split[1]));
                    productVO.setThreeCategoryId(Long.parseLong(split[2]));
                    criteria.andEqualTo("oneCategoryId",productVO.getOneCategoryId())
                            .andEqualTo("twoCategoryId",productVO.getTwoCategoryId())
                            .andEqualTo("threeCategoryId",productVO.getThreeCategoryId());
                    break;
                }
            }

        }
        //根据name判断
         String name = productVO.getName();
        if (!StringUtils.isEmpty(name)){
            criteria.andLike("name","%"+name+"%");
        }

        //根据status状态判断
        Integer status = productVO.getStatus();
        if (status != null){
            criteria.andEqualTo("status",status);
        }

        List<Product> productList = productMapper.selectByExample(example);
        //转成前端数据
        List<ProductVO> productVOList = productList.stream().map(product -> {
            ProductVO productVO1 = new ProductVO();
            BeanUtils.copyProperties(product,productVO1);

            return productVO1;
        }).collect(Collectors.toList());
        List<ProductVO> page = ListPageUtils.page(productVOList, pageSize, pageNum);
        if(page != null && page.size()>0){
            PageVO pageVO = new PageVO(productVOList.size(),page);
            return pageVO;
        }else {
            PageVO pageVO = new PageVO(productVOList.size(),productVOList);
            return pageVO;
        }
    }

    //添加物资
    @Override
    public void add(ProductVO productVO) {
        System.out.println("productVO = " + productVO);
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        if(productVO.getCategoryKeys().length>0){
            product.setOneCategoryId(productVO.getCategoryKeys()[0]);
            product.setTwoCategoryId(productVO.getCategoryKeys()[1]);
            product.setThreeCategoryId(productVO.getCategoryKeys()[2]);
        }
        Date time = Calendar.getInstance().getTime();
        product.setCreateTime(time);
        product.setModifiedTime(time);
        product.setStatus(2);
        product.setPNum(UUID.randomUUID().toString());
        productMapper.insert(product);
    }

    //编辑弹窗
    @Override
    public ProductVO edit(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        ProductVO productVO = new ProductVO();
        BeanUtils.copyProperties(product,productVO);
        return productVO;
    }

    //修改
    @Override
    public void update(Long id, ProductVO productVO) {
        productMapper.deleteByPrimaryKey(id);
        Product product = new Product();
        BeanUtils.copyProperties(productVO,product);
        product.setModifiedTime(new Date());
        if(productVO.getCategoryKeys().length>0){
            product.setOneCategoryId(productVO.getCategoryKeys()[0]);
            product.setTwoCategoryId(productVO.getCategoryKeys()[1]);
            product.setThreeCategoryId(productVO.getCategoryKeys()[2]);
        }
        productMapper.insert(product);

    }

    //移除回收站
    @Override
    public void remove(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        Product newProduct = new Product();
        BeanUtils.copyProperties(product,newProduct);
        productMapper.deleteByPrimaryKey(id);
        newProduct.setStatus(1);
        newProduct.setModifiedTime(new Date());
        newProduct.setId(id);
        productMapper.insert(newProduct);
    }

    //删除
    @Override
    public void delete(Long id) {
        productMapper.deleteByPrimaryKey(id);
    }

    //恢复
    @Override
    public void back(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        Product newProduct = new Product();
        BeanUtils.copyProperties(product,newProduct);
        productMapper.deleteByPrimaryKey(id);
        newProduct.setStatus(0);
        newProduct.setModifiedTime(new Date());
        newProduct.setId(id);
        productMapper.insert(newProduct);
    }

    //通过
    @Override
    public void publish(Long id) {
        Product product = productMapper.selectByPrimaryKey(id);
        Product newProduct = new Product();
        BeanUtils.copyProperties(product,newProduct);
        productMapper.deleteByPrimaryKey(id);
        newProduct.setStatus(0);
        newProduct.setModifiedTime(new Date());
        newProduct.setId(id);
        productMapper.insert(newProduct);
    }
}
